package logic

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"pharymacy-management-system/dao/mysql"
	"pharymacy-management-system/models"
	"pharymacy-management-system/network"
	"pharymacy-management-system/pkg/snowflake"
)

func GetAllDrugType(c *gin.Context) {
	typeList, err := mysql.SelectAllDrugType()
	if err != nil {
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	data := map[string]interface{}{}
	data["typeList"] = typeList
	network.ResponseSuccess(c, data)
}

func UpdateDrugTypeName(c *gin.Context) {
	var p models.ParamType
	if err := c.ShouldBindJSON(&p); err != nil {
		zap.L().Error("c.ShouldBindJSON(p) err", zap.Error(err))
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}
	drugType, err := mysql.SelectDrugTypeById(p.TypeID)
	if err != nil || drugType == nil {
		zap.L().Error("mysql.SelectDrugTypeById(p.TypeID) err", zap.Error(err))
		network.ResponseErrorWithMsg(c, network.CodeInvalidParam, "药品类型不存在")
		return
	}
	err = mysql.UpdateDrugTypeById(p)
	if err != nil {
		zap.L().Error("mysql.UpdateDrugTypeById(drugType) err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, nil)
}

func AddDrugType(c *gin.Context) {
	typeName := c.Param("typename")
	if typeName == "" || len(typeName) == 0 {
		network.ResponseErrorWithMsg(c, network.CodeInvalidParam, "药品类型不能为空")
		return
	}

	drugType, err := mysql.SelectDrugTypeByName(typeName)
	if err != nil {
		zap.L().Error("mysql.SelectDrugTypeByName(typeName) err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}

	if drugType != nil {
		// 药品类型已存在
		network.ResponseErrorWithMsg(c, network.CodeInvalidParam, "药品类型已存在")
		return
	}

	err = mysql.InsertDrugType(typeName)
	if err != nil {
		zap.L().Error("mysql.InsertDrugType(typeName) err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, nil)
}

func GetAllDrug(c *gin.Context) {
	drugs, err := mysql.SelectAllDrug()
	if err != nil {
		zap.L().Error("mysql.SelectAllDrug() err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	data := map[string]interface{}{}
	data["drugList"] = drugs
	network.ResponseSuccess(c, data)
}

func AddDrug(c *gin.Context) {
	var p models.ParamDrug
	if err := c.ShouldBindJSON(&p); err != nil {
		zap.L().Error("c.ShouldBindJSON(&p) err", zap.Error(err))
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}
	// 生成药品的UUID
	drugID := snowflake.GenID()
	drug := &models.Drug{
		DrugID:    drugID,
		DrugName:  p.DrugName,
		DrugSpecs: p.DrugSpecs,
		DrugPrice: p.DrugPrice,
	}
	drugType := &models.DrugAndType{
		TypeID: p.DrugTypeID,
		DrugID: drugID,
	}
	err := mysql.InsertDrug(drug, drugType)
	if err != nil {
		zap.L().Error("mysql.InsertDrug(drug, drugType) err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, nil)
}

func UpdateDrug(c *gin.Context) {
	var p models.ParamDrug

	if err := c.ShouldBindJSON(&p); err != nil {
		zap.L().Error("c.ShouldBindJSON(&p) err", zap.Error(err))
		network.ResponseError(c, network.CodeInvalidParam)
		return
	}

	drug := &models.Drug{
		DrugID:    p.DrugID,
		DrugName:  p.DrugName,
		DrugSpecs: p.DrugSpecs,
		DrugPrice: p.DrugPrice,
	}
	drugType := &models.DrugAndType{
		DrugID: p.DrugID,
		TypeID: p.DrugTypeID,
	}
	err := mysql.UpdateDrugById(drug, drugType)
	if err != nil {
		zap.L().Error("mysql.UpdateDrugById(drug, drugType) err", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, nil)
}
